Implementing User Authentication
Table of Contents
Implementing a Log-on
Now that your registration page functions, you can build the login page. Remember, the role of the login page is to obtain (through a form) the user's username and password. Then, it will compare these values with the records in tbl_users. If there's a match, the login script sets a session variable indicating that the user is logged in. If there is no match, the user is redirected to a login failed page.
-
Open and explore login.asp.
Like registration.asp, this page contains a web form. However, this web form has only two fields—one for username and one for password.
-
Click to select the Submit button, and use the Server Behaviors panel to begin adding a User Authentication > Log In User behavior.
This behavior will do all the work of both verifying that the user's credentials match a pair in the database; that a session variable will be set, if the log-on was successful; and that the user will be redirected to an appropriate location, based on whether the log-on was successful.
-
In the top quarter of the Log In User dialog box, verify that frm_login is the selected form, that username is set as the Username Field, and that pwd is set as the Password field. In the second quarter of the dialog box, choose user_auth as the Connection, tbl_users as the Table, username as the Username Column, and pwd as the Password Column.
With these settings, you provide parameters that the script needs to compare the authentication entered into the login form with the list of registered users in the database.
-
In the third quarter of the Log In User dialog box, enter secret.asp for the successful login redirection, and login_failed.asp for the failed login redirection. Ensure that Go To Previous URL is checked. In the fourth quarter of the dialog box, select Username, Password, and Access Level, and choose userGroup in the Get Level From menu. Click OK.
In this step, you are accomplishing two goals. First, you are specifying where the user should be redirected depending on the success or failure of the log-on.
The Go To Previous URL setting needs some explanation. There are two ways that users will access this login page. First, they can access it directly by following the Log In link on the homepage. But they'll also see this page if they try to access a page that requires log-on (such as secret.asp), and they haven't logged in yet. That is, the login page may intercept their progress in the site. Upon successful log-on, users won't want to be redirected to the homepage—not when they were requesting secret.asp. By checking this option, you add functionality to the script that ensures that once these users have logged in, they are redirected to the page they attempted to load before they logged in.
The second goal you are accomplishing is to create the separate user group functionality discussed earlier. This will enable you to distinguish between basic access users and full access users who have logged in. Since these access levels are stored in the userGroup field of tbl_users, you specify that information in the Get Level From menu.
Figure 5. The Log In User dialog box
-
Click the Submit button. In the Behaviors panel, click the New Behavior (+) button and add a Form Validation behavior that makes both fields required.
Figure 6. The Validate Form dialog box
Every form needs some mechanism for validation.
-
Save and upload the page.
ASP and PHP users are ready to test the registration and login functionalities. To do so, open register.asp, and create a profile. Then log in with any e-mail address as a username and password. As always, try to break the application as well. Register the same name twice. Try logging in with the wrong password, try typing your phone number instead of your e-mail address, and so on.
Figure 7. The Site Registration page
ColdFusion users have a quick chore to do before they can test the application: They need to create the Application.cfm file that governs the web application framework. ASP users don't have to worry about creating a global.asa file—ASP will handle session management just fine without it. Likewise, PHP should handle session management without further coding.